package com.example.controller;

import com.example.utils.Result;
import com.example.entity.Student;
import com.example.service.StudentService;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/student")
public class StudentController {

    @Resource
    StudentService studentService;

    // 添加学生信息
    @PostMapping("/add")
    public Result add(@RequestBody Student student, HttpSession session) {
        student.setTeaId((Integer) session.getAttribute("id"));
        studentService.add(student);
        return Result.success(); // 返回添加成功信息
    }

    // 删除学生信息
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id) {
        studentService.deleteById(id);
        return Result.success(); // 返回删除成功信息
    }

    // 更新学生信息
    @PutMapping("/update")
    public Result update(@RequestBody Student student) {
        studentService.updateById(student);
        return Result.success(); // 返回更新成功信息
    }

    // 分页查询学生信息列表
    @GetMapping("/selectPage")
    public Result selectPage(@RequestParam(defaultValue = "1") Integer pageNum,
                             @RequestParam(defaultValue = "10") Integer pageSize,
                             Student student, HttpSession session) {
        student.setTeaId((Integer) session.getAttribute("id"));
        PageInfo<Student> pageInfo = studentService.selectPage(pageNum, pageSize, student);
        return Result.success(pageInfo); // 返回分页查询结果
    }
}
